home *** CD-ROM | disk | FTP | other *** search
/ HPAVC / HPAVC CD-ROM.iso / TUT05NEW.ZIP / TUT5.CPP < prev    next >
C/C++ Source or Header  |  1994-12-28  |  14KB  |  362 lines

  1. /////////////////////////////////////////////////////////////////////////////
  2. //                                                                         //
  3. // TUTPROG5.CPP - VGA Trainer Program 5 (in Turbo C++ 3.0)                 //
  4. //                                                                         //
  5. // "The VGA Trainer Program" is written by Denthor of Asphyxia. However it //
  6. // was limited to Pascal only in its first run.  All I have done is taken  //
  7. // his original release, translated it to C++ and touched up a few things. //
  8. // I take absolutely no credit for the concepts presented in this code and //
  9. // am NOT the person to ask for help if you are having trouble.            //
  10. //                                                                         //
  11. // Program Notes : This program presents the basic demo scroller.          //
  12. //                                                                         //
  13. //                 If you are compiling this code command line, be sure to //
  14. //                 use the "-ml" parameter (large memory model).           //
  15. //                 Otherwise, the program will compile and link, but will  //
  16. //                 lock up your system.                                    //
  17. //                                                                         //
  18. // Author        : Grant Smith (Denthor) - denthor@beastie.cs.und.ac.za    //
  19. // Translator    : Christopher G. Mann   - r3cgm@dax.cc.uakron.edu         //
  20. //                                                                         //
  21. // Last Modified : December 27, 1994                                       //
  22. //                                                                         //
  23. /////////////////////////////////////////////////////////////////////////////
  24.  
  25. //               //
  26. // INCLUDE FILES //
  27. //               //
  28.  
  29.   #include <conio.h>
  30.                // clrscr(), getch(), kbhit()
  31.   #include <dos.h>
  32.                // MK_FP, geninterrupt()
  33.   #include <iostream.h>
  34.                // cout
  35.   #include <stdio.h>
  36.                // fopen(), fread(), fclose(), FILE
  37.   #include <stdlib.h>
  38.                // calloc(), free(), exit()
  39.  
  40. //           //
  41. // CONSTANTS //
  42. //           //
  43.  
  44.   const XSize = 16;        // the width  of our font
  45.   const YSize = 16;        // the height of our font
  46.   const NumLetters = 62;   // the number of characters in our font
  47.  
  48. //                     //
  49. // FUNCTION PROTOTYPES //
  50. //                     //
  51.  
  52.   // MODE SETTING FUNCTIONS
  53.   void SetMCGA();
  54.   void SetText();
  55.  
  56.   // UTILITY FUNCTIONS
  57.   void Pal(unsigned char ColorNo, unsigned char R,
  58.        unsigned char G,       unsigned char B);
  59.   void LoadPal(char *FileName);
  60.   void Putpixel(int x, int y, unsigned char Col, unsigned char *Where);
  61.   void WaitRetrace();
  62.   int  Exist(char *FileName);
  63.  
  64.   // MID-LEVEL FUNCTIONS
  65.   void Setup();
  66.   void ScrollMsg (char *Msg, int SizeMsg);
  67.  
  68. //                 //
  69. // GLOBAL TYPEDEFs //
  70. //                 //
  71.  
  72.   typedef unsigned char Letter[XSize][YSize];
  73.  
  74. //                              //
  75. // GLOBAL VARIABLE DECLARATIONS //
  76. //                              //
  77.  
  78.   // pointer to the offset of the VGA memory
  79.   unsigned char *vga = (unsigned char *) MK_FP(0xA000, 0);
  80.  
  81.   // pointer to our index of font data
  82.   Letter *Font;
  83.  
  84.  
  85. ///////////////////////////////////////////////////////////////////////////////
  86. //                                                                           //
  87. //                                MAIN FUNCTION                              //
  88. //                                                                           //
  89. ///////////////////////////////////////////////////////////////////////////////
  90.  
  91. void main() {
  92.  
  93.   clrscr();
  94.   cout
  95.     << "This program will give you an example of a scrolly. If the file\n"
  96.     << "SOFTROCK.FNT is in the current directory, this program will scroll\n"
  97.     << "letters, otherwise it will only scroll bars. It also searches for\n"
  98.     << "PALLETTE.COL, which it uses for it''s pallette. Both SOFTROCK.FNT\n"
  99.     << "and PALLETTE.COL come with TEXTER5.ZIP, at a BBS near you.\n\n";
  100.   cout
  101.     << "You will note that you can change what the scrolly says merely by\n"
  102.     << "changing the string in the program.\n\n";
  103.  
  104.   Setup();
  105.  
  106.   do ScrollMsg ("ASPHYXIA RULZ!!!   ",19); while (!kbhit());
  107.   getch();
  108.  
  109.   SetText();
  110.   free(Font);
  111.  
  112.   cout
  113.     << "All done. This concludes the fifth sample program in the ASPHYXIA\n"
  114.     << "Training series. You may reach DENTHOR under the name of GRANT\n"
  115.     << "SMITH on the MailBox BBS, or leave a message to ASPHYXIA on the\n"
  116.     << "ASPHYXIA BBS. Get the numbers from Roblist, or write to :\n"
  117.     << "             Grant Smith\n"
  118.     << "             P.O. Box 270\n"
  119.     << "             Kloof\n"
  120.     << "             3640\n"
  121.     << "I hope to hear from you soon!\n\n";
  122.   cout
  123.     << "Hit any key to exit ...";
  124.   getch();
  125.  
  126. }
  127.  
  128.  
  129. /////////////////////////////////////////////////////////////////////////////
  130. //                                                                         //
  131. // SetMCGA() - This function gets you into 320x200x256 mode.               //
  132. //                                                                         //
  133. /////////////////////////////////////////////////////////////////////////////
  134.  
  135. void SetMCGA() {
  136.   _AX = 0x0013;
  137.   geninterrupt (0x10);
  138. }
  139.  
  140.  
  141. /////////////////////////////////////////////////////////////////////////////
  142. //                                                                         //
  143. // SetText() - This function gets you into text mode.                      //
  144. //                                                                         //
  145. /////////////////////////////////////////////////////////////////////////////
  146.  
  147. void SetText() {
  148.   _AX = 0x0003;
  149.   geninterrupt (0x10);
  150. }
  151.  
  152.  
  153. /////////////////////////////////////////////////////////////////////////////
  154. //                                                                         //
  155. // Pal() - This sets the Red, Green, and Blue values of a certain color.   //
  156. //                                                                         //
  157. /////////////////////////////////////////////////////////////////////////////
  158.  
  159. void Pal(unsigned char ColorNo, unsigned char R,
  160.      unsigned char G,       unsigned char B) {
  161.  
  162.   outp (0x03C8,ColorNo); // here is the pallette color I want to set
  163.   outp (0x03C9,R);
  164.   outp (0x03C9,G);
  165.   outp (0x03C9,B);
  166.  
  167. }
  168.  
  169.  
  170. /////////////////////////////////////////////////////////////////////////////
  171. //                                                                         //
  172. // LoadPal() - This loads the Pallette file and puts it on screen.         //
  173. //                                                                         //
  174. /////////////////////////////////////////////////////////////////////////////
  175.  
  176. void LoadPal(char *FileName) {
  177.  
  178.   typedef unsigned char DACType[256][3]; // [256] colors, [3] types (R,G,B)
  179.   DACType DAC;
  180.   FILE *fp;
  181.   int loop1;
  182.  
  183.   fp = fopen(FileName,"rb");
  184.   fread(DAC,sizeof(DACType),1,fp);
  185.   fclose(fp);
  186.  
  187.   for (loop1=0; loop1<256; loop1++)
  188.     Pal(loop1, DAC[loop1][0], DAC[loop1][1], DAC[loop1][2]);
  189.  
  190. }
  191.  
  192.  
  193. /////////////////////////////////////////////////////////////////////////////
  194. //                                                                         //
  195. // Putpixel() - This puts a pixel at X,Y using color Col, on VGA or the    //
  196. //              Virtual Screen;                                            //
  197. //                                                                         //
  198. /////////////////////////////////////////////////////////////////////////////
  199.  
  200. void Putpixel (int x, int y, unsigned char Col, unsigned char *Where) {
  201.   memset(Where+(x+(y*320)),Col,1);
  202. }
  203.  
  204.  
  205. /////////////////////////////////////////////////////////////////////////////
  206. //                                                                         //
  207. // WaitRetrace() - This waits until you are in a Verticle Retrace.         //
  208. //                                                                         //
  209. /////////////////////////////////////////////////////////////////////////////
  210.  
  211. void WaitRetrace() {
  212.  
  213.   _DX = 0x03DA;
  214.  
  215.   l1: asm {
  216.     in  al,dx;
  217.     and al,0x08;
  218.     jnz l1;
  219.       }
  220.  
  221.   l2: asm {
  222.     in  al,dx;
  223.     and al,0x08;
  224.     jz  l2;
  225.       }
  226. }
  227.  
  228.  
  229. /////////////////////////////////////////////////////////////////////////////
  230. //                                                                         //
  231. // Exist() - Checks to see if file exists or not.                          //
  232. //                                                                         //
  233. /////////////////////////////////////////////////////////////////////////////
  234.  
  235. int Exist (char *FileName) {
  236.  
  237.   FILE *fp;
  238.  
  239.   if ((fp = fopen(FileName,"rb")) != NULL) {
  240.     fclose(fp);
  241.     return 1;
  242.   }
  243.   else
  244.     return 0;
  245.  
  246. }
  247.  
  248.  
  249. /////////////////////////////////////////////////////////////////////////////
  250. //                                                                         //
  251. // Setup() - This loads the font and the pallette.                         //
  252. //                                                                         //
  253. /////////////////////////////////////////////////////////////////////////////
  254.  
  255. void Setup() {
  256.  
  257.   FILE *fp;
  258.   int loop1, loop2, loop3;
  259.  
  260.   Font = (Letter *) calloc(NumLetters,sizeof(Letter));
  261.  
  262.     if (Font == NULL) { // always check to make sure you have enough memory!
  263.       SetText();
  264.       cout << "Not enough memory available, exiting program...";
  265.       exit(1);
  266.     }
  267.  
  268.   if (Exist("SOFTROCK.FNT\0")) { // don't forget the "\0" null terminator
  269.     fp = fopen("SOFTROCK.FNT","rb");
  270.     fread(Font,sizeof(Letter),NumLetters,fp);
  271.     cout << "SoftRock.FNT from TEXTER5 found in current directory. Using.\n";
  272.     fclose(fp);
  273.   }
  274.   else {
  275.     cout << "SoftRock.FNT from TEXTER5 not found in current directory.\n";
  276.     for     (loop1=0; loop1<NumLetters; loop1++)
  277.       for   (loop2=0; loop2<XSize;      loop2++)
  278.     for (loop3=0; loop3<YSize;      loop3++)
  279.       Font[loop1][loop2][loop3] = loop2;
  280.   }
  281.  
  282.   if (Exist("PALLETTE.COL\0")) // don't forget the \0 null terminator
  283.     cout << "Pallette.COL from TEXTER5 found in current directory. Using.\n";
  284.   else
  285.     cout << "Pallette.COL from TEXTER5 not found in current directory.\n";
  286.  
  287.   cout << "\n\nHit any key to continue ...";
  288.   getch();
  289.  
  290.   SetMCGA();
  291.  
  292.   if (Exist("PALLETTE.COL\0")) LoadPal("PALLETTE.COL\0");
  293. }
  294.  
  295.  
  296. /////////////////////////////////////////////////////////////////////////////
  297. //                                                                         //
  298. // ScrollMsg() - This scrolls the string in Msg across the screen.         //
  299. //                                                                         //
  300. /////////////////////////////////////////////////////////////////////////////
  301.  
  302. void ScrollMsg (char *Msg, int SizeMsg) {
  303.  
  304.   int loop1, loop2, loop3;
  305.   int Location = 100; // specify how far from the top of the screen we want
  306.               // the scroller to appear
  307.  
  308.   // MAIN LOOP - do every letter in Msg
  309.   for (loop1 = 0; loop1 < SizeMsg; loop1++) {
  310.  
  311.     // Do this loop <n> number of times where <n> = the width of a letter
  312.     for (loop2 = 0; loop2 < XSize; loop2++) {
  313.  
  314.     // There are two loops here.
  315.     //
  316.     // 1. Copy YSize rows to the left by 1.  In effect, this frees up a
  317.     //    blank column of vertical pixels on the right side of the screen.
  318.     // 2. Replace that new blank column with pixels from our Font[] table.
  319.  
  320.     WaitRetrace();
  321.  
  322.     for (loop3=Location; loop3<Location+YSize; loop3++)
  323.       // move each row to the left by 1
  324.       memcpy(vga+(0+(loop3*320)), vga+(1+(loop3*320)), 320);
  325.  
  326.     for (loop3=Location; loop3<Location+YSize; loop3++)
  327.       // Replace the new column on the right with information from Font[].
  328.       // In the following statement:
  329.       //   (320,         - y location (the rightmost column)
  330.       //   loop3,        - x location (the current row we are working with)
  331.       //   Font[...      - Get information from our Font table.  All of this
  332.       //                   complex indexing really just gives us the pallette
  333.       //                   number we are putting to the screen at a given
  334.       //                   location.
  335.       //    [(Msg[loop1]-32)] - Access the correct letter from our Font
  336.       //                        table.  The (Msg[loop1]) would give us the
  337.       //                        ASCII value of the letter we want to display,
  338.       //                        but we need to subtract 32 to coordinate the
  339.       //                        ASCII value with our Font table.
  340.       //    [loop2]           - the column index of the letter we are using
  341.       //    [loop3-Location]  - the row    index of the letter we are using
  342.       Putpixel (319,loop3,Font[(Msg[loop1]-32)][loop2][loop3-Location],vga);
  343.  
  344.     } // we have now inserted a new letter on the right side of the screen
  345.  
  346.   // This next bit scrolls by one pixel after each letter so that there
  347.   // are gaps between the letters
  348.  
  349.   WaitRetrace();
  350.  
  351.   for (loop3=Location; loop3<Location+YSize; loop3++)
  352.     // move each row to the left by 1 (same as above)
  353.     memcpy( vga+(loop3*320), vga+(1+(loop3*320)), 320);
  354.  
  355.   for (loop3 = 100; loop3 < 100 + YSize; loop3++)
  356.     // put a blank column of pixels
  357.     Putpixel(319,loop3,0,vga);
  358.  
  359.   } // we have now scrolled the entire message once
  360.  
  361. }
  362.